package com.easemob.helpdesk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.easemob.helpdesk.bean.DiscountCouponBean;
import com.google.gson.Gson;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

/**
 * Created by xiaolong.wei on 2018/10/17.
 */

public class ScanQRCodActivity extends BaseActivityV2 {

    private static final String TAG = "ScanQRCodActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setBeepEnabled(false);
        intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        intentIntegrator.setCaptureActivity(CustomCaptureActivity.class);
        // 开始扫描
        intentIntegrator.initiateScan();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 获取解析结果
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                finish();
                Toast.makeText(this, "取消扫描", Toast.LENGTH_LONG).show();
            } else {
                String qrCodeContent = result.getContents();
                if (TextUtils.isEmpty(qrCodeContent)){
                    Toast.makeText(this,"券无效，请联系客服",Toast.LENGTH_LONG).show();
                }else {
                    Intent intent = new Intent(this,ScanQRCodeResultActivity.class);
                    intent.putExtra("DISCOUNT_COUPON",qrCodeContent);
                    startActivity(intent);
                }
                finish();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
